import { Assets, ImageSpec } from "../consts/Assets";
import { EventNames } from "../consts/EventNames";
import EventBus from "../core/EventBus";
import ScreenManager from "../core/ScreenManager";

export enum PlayerStatus {
    IDLE,
    AUTO_RUN,
    FIGHTING
}

declare var ImageClassifiers: any;

class StatusChecker {
    private checkValue: number = 0;
    private ic = ImageClassifiers.create("/sdcard/Pictures/tf/converted_tflite/model_unquant.tflite","/sdcard/Pictures/tf/converted_tflite/labels.txt");
    constructor(public bindingStatus: PlayerStatus, private imgSpec: ImageSpec) {
        this.checkValue = 0;
    }

    public onUpdate(): boolean {
        let status = ScreenManager.recognizeImage(this.ic, 940,680,120,120);
        if (status == PlayerStatus[PlayerStatus.AUTO_RUN]) {
            return true;
        }

        return false;

        // if (ScreenManager.matchImage(this.imgSpec)) {
        //     this.checkValue = 4;
        // } else {
        //     this.checkValue--;
        // }
        // return this.checkValue > 0;
    }
}

export class PlayerStatusManager {
    private static STATUS_TEXT = [
        "空闲",
        "自动寻路中...",
        "自动战斗中..."
    ]
    public static previousStatus: PlayerStatus = PlayerStatus.IDLE;
    public static currentStatus: PlayerStatus = PlayerStatus.IDLE;
    public static statusCheckers: StatusChecker[] = [
        new StatusChecker(PlayerStatus.AUTO_RUN, Assets.images.img_zi),
        //new StatusChecker(PlayerStatus.FIGHTING, Assets.images.btn_show_map),
    ];

    public static updateChecker(): void {
        this.currentStatus = PlayerStatus.IDLE;

        this.statusCheckers.some(el => {
            let checked = el.onUpdate();
            if (checked) {
                this.currentStatus = el.bindingStatus;
            }
            return checked;
        });

        this.updateStatusText();
    }

    private static updateStatusText(): void {
        if (this.previousStatus != this.currentStatus) {
            this.previousStatus = this.currentStatus;
            EventBus.emit(EventNames.UPDATE_STATUS_TEXT, this.STATUS_TEXT[this.currentStatus]);
        }
    }
}